#ifndef NETSERVICE_H
#define NETSERVICE_H

#include "../global.h"
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/asio/detail/config.hpp>

class TcpConnectionFactory;
class TcpAcceptor;
class AsyncService;

class NetService : protected boost::noncopyable
{
public:
    typedef boost::shared_ptr< TcpConnectionFactory > ConnFactoryPtr;
    typedef boost::shared_ptr< TcpAcceptor > AcceptorPtr;

    typedef boost::function< size_t (size_t) > Filter;
    typedef boost::function< void (size_t, int) > Completion;
    typedef boost::function< void (unsigned long, int) > AcceptNotifier;

public:
    NetService(AsyncService &service);
    ~NetService();

    int  init();

    int  open(const std::string &host, unsigned short port, int listenMax);

    void close();

    void close(unsigned long key);

    int  localEndpoint(std::string &host, unsigned short &port);

    int  remoteEndpoint(std::string &host, unsigned short &port);

    void read(unsigned long key, void *buf, size_t bs,
              BOOST_ASIO_MOVE_ARG(Filter) f,
              BOOST_ASIO_MOVE_ARG(Completion) c);

    void write(unsigned long key, const void *buf, size_t bs,
               BOOST_ASIO_MOVE_ARG(Filter) f,
               BOOST_ASIO_MOVE_ARG(Completion) c);

    void accept(BOOST_ASIO_MOVE_ARG(AcceptNotifier) a);

protected:
    AsyncService &m_service;

    AcceptorPtr m_acceptor;

    ConnFactoryPtr m_connectionFactory;
};

#endif // NETSERVICE_H
